package test.ground.monitor;

import com.alibaba.ttl.threadpool.TtlExecutors;
import com.wang.tool.monitor.InvokeTemplate;
import com.wang.tool.monitor.aop.AnnotationWeaver;
import com.wang.tool.monitor.core.LinkHolder;
import com.wang.tool.monitor.core.MonitorInfo;
import com.wang.tool.monitor.template.AbsMonitorInvoker;
import com.wang.tool.monitor.template.MonitorInvoker;
import com.wang.tool.okhttp.HttpTemplate;
import com.wang.tool.trace.TraceTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import test.ground.kafka.KafkaProducerService;
import test.ground.model.House;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * @Author wangshaopeng
 * @Date 2022/7/2
 */
@Controller
@RequestMapping("/monitor")
@ConfigurationProperties
public class MonitorC extends MC<ServiceA.Result2>{

    @Autowired
    private ServiceA serviceA;

    @Autowired
    private ServiceB serviceB;

    @Autowired
    private ServiceC serviceC;


    public void g1(){

    }

    @Autowired
    private KafkaProducerService kafkaProducerService;

    private Executor executor = TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(30));

    @RequestMapping("/t2")
    public ServiceA.Result2 t2(String k) throws InterruptedException {
        InvokeTemplate.monitor("MonitorC", "t22", new MonitorInvoker<Integer>(k) {

            @Override
            public void weaveOnBefore(MonitorInfo<Integer> monitorInfo) throws Throwable {
                monitorInfo.markDebug(true);

            }

            @Override
            public Integer internalCall() throws Throwable {
                LinkHolder.addTips(()->"这时测试调试西溪");

                return 2;
            }
        });
        return new ServiceA.Result2();
    }

    @RequestMapping("/t1")
    public void t1(House k) throws InterruptedException {
        List<Integer> x = InvokeTemplate.monitor("MonitorC", "t1", new AbsMonitorInvoker<List<Integer>>() {
            @Override
            protected void internalAfter(MonitorInfo<List<Integer>> monitorInfo) {
                monitorInfo.addDigest("te", "2");
                monitorInfo.addDetail("debugs",monitorInfo.buildTipsMap());
                System.out.println(monitorInfo.getLink().toProfilerString());
            }

            @Override
            public List<Integer> internalCall() throws Exception {
                for (int i = 0; i < 1; i++) {
//                    InvokeTemplate.addDebugInfo("testkey","valavalval");
//                    serviceC.hi(k);
                }
                LinkHolder.addTips(()->"这时测试调试西溪"+ LinkHolder.getCurrent().getLinkid());
                return new ArrayList<>();
            }

            @Override
            public void weaveOnBefore(MonitorInfo<List<Integer>> monitorInfo) {
//                monitorInfo.setDebug(true);
                monitorInfo.addDigest("te", "2");
                monitorInfo.addDigest("t2", "3");

//                monitorInfo.setSlowTime(10);
            }
        });
        System.out.println(x);

//
////        SimpleLogUtil.info(SimpleLogUtil.SIMPLE_LOGGER,"11245");
//         InvokeTemplate.invoke("t1","MonitorC","t1", new MonitorInvoker<Integer>() {
//
//
//            @Override
//            public Integer internalCall() throws Exception {
////                LinkLogUtil.newEventLog("测试事件").t1("事件补充").log();
////                SimpleLogger.info(LinkLogUtil.BIZ_LOGGER,"111");
////                LinkLogUtil.error(LinkLogUtil.BIZ_LOGGER,new IllegalArgumentException("测试异常"),"1111");
////                executor.execute(()->{
////                    serviceA.hi(k);
//////                    serviceB.hi();
//////                    serviceC.hi();
////                });
//                for (int i = 0; i < 1; i++) {
//                    serviceC.hi();
////                    executor.execute(()->{
////                    });
//                }
////                Thread.sleep(10);
////                serviceB.hi();
//                return 2;
//            }
//
//             @Override
//             public void weaveOnBefore(MonitorInfo<Integer> monitorInfo) {
//                monitorInfo.addDigest("测试摘要","债啊哟");
////                 monitorInfo.addDigest("测试摘要2",null);
//             }
//
//             @Override
//             public void weaveOnAfter(MonitorInfo<Integer> monitorInfo) {
//
//             }
//             //             @Override
////             public void injectMonitor(MonitorInfo<Integer> monitorInfo) {
////                 PrepareApplication.inject(monitorInfo);
////                 monitorInfo.addLinkInfoPayload();
////             }
//         });
//        return null;

    }

    @RequestMapping("/t3")
    public ServiceA.Result2 t3(String k) throws InterruptedException {
        TraceTemplate.monitorWithNext("http","t33_1","next",new AbsMonitorInvoker<String>() {
            @Override
            public String internalCall() throws Throwable {
                return Integer.parseInt(k)+"_1";
            }
        });
        InvokeTemplate.monitor("http","t33_1","next",new AbsMonitorInvoker<String>() {
            @Override
            public String internalCall() throws Throwable {
                return Integer.parseInt(k)+"_1";
            }
        });
        HttpTemplate.get("http://localhost:70/getRefresh?key="+k);

        kafkaProducerService.sendMessage("hi_"+k);
        return new ServiceA.Result2();
    }


    public static class Test implements AnnotationWeaver {

        @Override
        public void init(String group, String biz, String action, boolean printParam, boolean printResult, Map config) throws Throwable {

        }

        @Override
        public void weaveOnBefore(MonitorInfo monitorInfo) {
            System.out.println("kaishi");
            monitorInfo.markDebug(true);
        }

        @Override
        public void weaveOnAfter(MonitorInfo monitorInfo) {
            System.out.println("结束");
        }
    }


}
